package com.skcc.ts.nsi.expense;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.MEDataHandler;
import com.skcc.ts.nsi.service.SocketClientService;

public class MEExpenseRegister extends Activity {
	private ArrayList<String[]> pjtList = null;
	private ArrayList<String[]> acntList = null;
	private Button btn_expense_register;

	private int expSearchid = 45; // 정산내역등록 45
	private TextView tv_billDate;
	private TextView tv_shopName;
	private TextView tv_billAmount;

	private boolean sms;
	private String myId;
	private String billCode;
	private String billDate;
	private String billAmount;
	private String shopName;
	private String shopLocation;
	private String state;
	private String processDate;
	private String type;
	private String selectedPjt;
	private String selectedAcnt;
	private String pjtCode;
	private String acntCode;
	private String addr;
	private String port;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meexpense_register);

		tv_billDate = (TextView) findViewById(R.id.textView_expense_mjs_register_date);
		tv_shopName = (TextView) findViewById(R.id.textView_expense_mjs_register_shop);
		tv_billAmount = (TextView) findViewById(R.id.textView_expense_mjs_register_sum);

		getRegiInfo(); // 등록정보세팅
		doListExpense(); // 프로젝트정보수집
		initSpinners(); // 스니퍼 동적생성
		initButtons(); // 버튼 세팅

	}

	private void getRegiInfo() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("expRegi", false);
		boolean smsYN = receiveI.getBooleanExtra("SMS", false);

		myId = receiveI.getStringExtra("myId");
		billCode = receiveI.getStringExtra("billCode");
		billDate = receiveI.getStringExtra("billDate");
		billAmount = receiveI.getStringExtra("billAmount");
		shopName = receiveI.getStringExtra("shopName");
		shopLocation = receiveI.getStringExtra("shopLocation");
		state = receiveI.getStringExtra("state");
		processDate = receiveI.getStringExtra("processDate");
		type = receiveI.getStringExtra("type");
		addr = receiveI.getStringExtra("loginAddr");
		port = receiveI.getStringExtra("loginPort");

		if (doYN) {
			pjtList = new ArrayList<String[]>();
			acntList = new ArrayList<String[]>();
			// myId = receiveI.getStringExtra("myId");
			String pCode = "-- 선택 --,";
			String pName = "-- 선택 --,";
			String aCode = "-- 선택 --,";
			String aName = "-- 선택 --,";
			if (smsYN) {
				sms = true;
			}
			String[] pjt_code = receiveI.getStringArrayExtra("pjtCode");
			String[] pjt_name = receiveI.getStringArrayExtra("pjtName");
			String[] acnt_code = receiveI.getStringArrayExtra("acntCode");
			String[] acnt_name = receiveI.getStringArrayExtra("acntName");
			for (int i = 0; i < pjt_code.length; i++) {
				pCode = pCode.concat(pjt_code[i] + ",");
				pName = pName.concat(pjt_name[i] + ",");
			}
			for (int i = 0; i < acnt_code.length; i++) {
				aCode = aCode.concat(acnt_code[i] + ",");
				aName = aName.concat(acnt_name[i] + ",");
			}
			String[] pjtCode = MEDataHandler.checkArray(pCode);
			String[] pjtName = MEDataHandler.checkArray(pName);
			String[] acntCode = MEDataHandler.checkArray(aCode);
			String[] acntName = MEDataHandler.checkArray(aName);
			pjtList.add(0, pjtCode);
			pjtList.add(1, pjtName);
			acntList.add(0, acntCode);
			acntList.add(1, acntName);
			Log.d("expensRegister:리스팅완료", "yse");

			tv_billDate.setText(billDate.substring(0, 10).replace("-", "/"));
			tv_billDate.setGravity(Gravity.CENTER);
			tv_billDate.setTextSize(20);
			tv_billDate.setTextColor(Color.BLACK);
			tv_shopName.setText(shopName);
			tv_shopName.setGravity(Gravity.CENTER);
			tv_shopName.setTextSize(20);
			tv_shopName.setTextColor(Color.BLACK);
			tv_billAmount.setText(MEDataHandler.addComma(billAmount) + "원");
			tv_billAmount.setGravity(Gravity.CENTER);
			tv_billAmount.setTextSize(20);
			tv_billAmount.setTextColor(Color.BLACK);
		}
	}

	private void doListExpense() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		if (doYN) {
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			boolean reset = receiveI.getBooleanExtra("setAcnt", false);
			pjtList = new ArrayList<String[]>();
			acntList = new ArrayList<String[]>();
			String pCode = "-- 선택 --,";
			String pName = "-- 선택 --,";
			String aCode = "-- 선택 --,";
			String aName = "-- 선택 --,";
			String[] pjt_code = receiveI.getStringArrayExtra("projectCode");
			String[] pjt_name = receiveI.getStringArrayExtra("projectName");
			String[] acnt_code = receiveI.getStringArrayExtra("accountCode");
			String[] acnt_name = receiveI.getStringArrayExtra("accountName");
			for (int i = 0; i < pjt_code.length; i++) {
				pCode = pCode.concat(pjt_code[i] + ",");
				pName = pName.concat(pjt_name[i] + ",");
			}
			for (int i = 0; i < acnt_code.length; i++) {
				aCode = aCode.concat(acnt_code[i] + ",");
				aName = aName.concat(acnt_name[i] + ",");
			}
			String[] pjtCode = MEDataHandler.checkArray(pCode);
			String[] pjtName = MEDataHandler.checkArray(pName);
			String[] acntCode = MEDataHandler.checkArray(aCode);
			String[] acntName = MEDataHandler.checkArray(aName);
			pjtList.add(0, pjtCode);
			pjtList.add(1, pjtName);
			acntList.add(0, acntCode);
			acntList.add(1, acntName);
			if (reset) {
				new AlertDialog.Builder(MEExpenseRegister.this).setTitle("알림").setMessage("프로젝트 내 해당계정이 없습니다.")
						.setPositiveButton("OK", null).show();
			}
		}
	}

	private void initSpinners() {// 스피너 세팅
		final String[] pjtSnipper = pjtList.get(1);
		final String[] acntSnipper = acntList.get(1);
		ArrayAdapter<String> adapter_pjt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pjtSnipper);
		ArrayAdapter<String> adapter_acnt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, acntSnipper);

		Spinner sp_pjt = (Spinner) findViewById(R.id.spinner_expense_register_pjtlist);
		adapter_pjt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_pjt.setAdapter(adapter_pjt);

		sp_pjt.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedPjt = pjtSnipper[position];
				pjtCode = pjtList.get(0)[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedPjt = "-- 선택 --";
			}
		});

		Spinner sp_acnt = (Spinner) findViewById(R.id.spinner_expense_register_acntlist);
		adapter_acnt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_acnt.setAdapter(adapter_acnt);

		sp_acnt.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedAcnt = acntSnipper[position];
				acntCode = acntList.get(0)[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedAcnt = "-- 선택 --";
			}
		});
	}

	/**
	 * <b>메소드 :</b> <b>메소드 한글/영문 명을 여기에 기입합니다.</b>
	 * <p>
	 * <ul>
	 * <li>버튼 이벤트 정리
	 * </ul>
	 * </p>
	 * 
	 * @since 2012. 8. 14.
	 */
	private void initButtons() {
		btn_expense_register = (Button) findViewById(R.id.btn_expense_register);

		// 정산내역 등록
		btn_expense_register.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {

				if ("-- 선택 --".equals(pjtCode) && !("-- 선택 --".equals(acntCode))) {
					new AlertDialog.Builder(MEExpenseRegister.this).setTitle("알림").setMessage("프로젝트를 선택하여야 계정선택이 가능합니다.")
							.setPositiveButton("OK", null).show();
				} else if (!("-- 선택 --".equals(pjtCode)) && "-- 선택 --".equals(acntCode)) {
					new AlertDialog.Builder(MEExpenseRegister.this).setTitle("알림").setMessage("계정을 선택하여 주십시오.")
							.setPositiveButton("OK", null).show();
				} else if ("-- 선택 --".equals(pjtCode) || "-- 선택 --".equals(acntCode)) {
					new AlertDialog.Builder(MEExpenseRegister.this).setTitle("알림").setMessage("프로젝트와  계정을 선택하여 주십시오.")
							.setPositiveButton("OK", null).show();
				} else {

					Intent expI = new Intent(MEExpenseRegister.this, SocketClientService.class);
					expI.putExtra("id", expSearchid);// 45 정산내역등록
					expI.putExtra("myId", myId);
					expI.putExtra("sms", sms);
					expI.putExtra("code", billCode);
					// expI.putExtra("day", billDate);
					expI.putExtra("projectCode", pjtCode);
					expI.putExtra("accountCode", acntCode);
					expI.putExtra("accountName", selectedAcnt);
					expI.putExtra("money", billAmount);
					expI.putExtra("loginAddr", addr);
					expI.putExtra("loginPort", port);
					expI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startService(expI);
					finish();
				}
			}
		});
	}

}